package edu.zut.servlet;

import edu.zut.pojo.SysUser;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

@WebServlet(name = "JSPServlet2", value = "/jsp/demo2")
public class JSPServlet2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        request.setAttribute("time", new Date());
        HttpSession session = request.getSession();
        session.setAttribute("address", "中原路中原工学院");
        ServletContext context = request.getServletContext();
        context.setAttribute("school", "中原工学院");
        Cookie cookie = new Cookie("myName","xueyin");
        response.addCookie(cookie);
        //保存对象
        SysUser sysUser = new SysUser();
        sysUser.setName("jack");
        sysUser.setAge(21);
        request.setAttribute("sysUser",sysUser);
        //保存集合list（数组）或map
        ArrayList<Object> list = new ArrayList<>();
        list.add("21");
        list.add("20");
        list.add("22");
        request.setAttribute("list",list);
        //保存map集合
        HashMap<Object, Object> map = new HashMap<>();
        map.put("k1","v1");
        map.put("k2","v2");
        map.put("k3","v3");
        request.setAttribute("map",map);
        //转发
        request.getRequestDispatcher("/demo2.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
